﻿using System;
using System.Management.Automation;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.PowerShell;

namespace MS.InformationManagement.DocumentIDServiceApp.Registration
{
    [Cmdlet(VerbsLifecycle.Install, "DocumentIDServiceProxy", SupportsShouldProcess = true)]
    internal sealed class InstallDocumentIDServiceProxy : SPCmdlet
    {
        protected override bool RequireUserFarmAdmin()
        {
            return true;
        }

        protected override void InternalProcessRecord()
        {
            // Ensure that the required local farm object is available
            SPFarm farm = SPFarm.Local;
            if (null == farm)
            {
                ThrowTerminatingError(new InvalidOperationException("SharePoint server farm not found."),
                    ErrorCategory.ResourceUnavailable, this);
            }

            if (ShouldProcess("DocumentID Web Service Proxy"))
            {
                // Ensure that the sample web service proxy exists
                DocumentIDServiceProxy serviceProxy = farm.ServiceProxies.GetValue<DocumentIDServiceProxy>();
                if (null == serviceProxy)
                {
                    // Create the service proxy
                    serviceProxy = new DocumentIDServiceProxy(farm);
                    serviceProxy.Update(true);
                }
            }
        }
    }
}
